//
//  WJCenterPopupView.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/20.
//

import UIKit

class WJCenterPopupView: WJBasePopupView {
    
    private let titleLabel = UILabel()
    private let messageLabel = UILabel()
    private let closeButton = UIButton()
    private let actionButton = UIButton()
    
    var onAction: (() -> Void)?
    
    override func setupContent() {
        super.setupContent()
        setupContentView()
        setupConstraints()
    }
    
    private func setupContentView() {
        // 关闭按钮
        closeButton.setImage(UIImage(systemName: "xmark"), for: .normal)
        closeButton.tintColor = .gray
        closeButton.addTarget(self, action: #selector(closeTapped), for: .touchUpInside)
        contentView.addSubview(closeButton)
        
        // 标题
        titleLabel.text = "温馨提示"
        titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
        titleLabel.textAlignment = .center
        titleLabel.textColor = .black
        contentView.addSubview(titleLabel)
        
        // 消息
        messageLabel.text = "这是一个从中间弹出的提示框，支持自定义内容"
        messageLabel.font = UIFont.systemFont(ofSize: 16)
        messageLabel.textAlignment = .center
        messageLabel.textColor = .darkGray
        messageLabel.numberOfLines = 0
        contentView.addSubview(messageLabel)
        
        // 操作按钮
        actionButton.setTitle("立即操作", for: .normal)
        actionButton.backgroundColor = .systemBlue
        actionButton.setTitleColor(.white, for: .normal)
        actionButton.layer.cornerRadius = 8
        actionButton.addTarget(self, action: #selector(actionTapped), for: .touchUpInside)
        contentView.addSubview(actionButton)
    }
    
    private func setupConstraints() {
        closeButton.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        actionButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            // 关闭按钮
            closeButton.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 15),
            closeButton.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -15),
            closeButton.widthAnchor.constraint(equalToConstant: 30),
            closeButton.heightAnchor.constraint(equalToConstant: 30),
            
            // 标题
            titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 25),
            titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            
            // 消息
            messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 15),
            messageLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            messageLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            
            // 操作按钮
            actionButton.topAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 25),
            actionButton.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            actionButton.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            actionButton.heightAnchor.constraint(equalToConstant: 50),
            actionButton.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20)
        ])
    }
    
    func configure(title: String, message: String, actionText: String = "立即操作") {
        titleLabel.text = title
        messageLabel.text = message
        actionButton.setTitle(actionText, for: .normal)
    }
    
    @objc private func closeTapped() {
        dismiss()
    }
    
    @objc private func actionTapped() {
        dismiss {
            self.onAction?()
        }
    }
}
